package interfaces;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;

import model.EditType;
import model.UserDataWrapper;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import services.User;
import services.UserDataServicesProxy;

import com.toedter.calendar.JDateChooser;

public class UserDataEditDialog  extends JPanel {
	private static final long serialVersionUID = 1L;
	private JDateChooser date;
	private JSpinner startTimeSpinner;
	private JSpinner endTimeSpinner;
	private JTextField note = new JTextField(10);
	private JButton okButton = new JButton("Ок");
	private JButton cancelButton = new JButton("Отмена");
	private JDialog dialog;
	private UserDataWrapper result;
	private User user;
	private EditType type;
	public UserDataEditDialog(JDialog dialog, User user, UserDataWrapper data, EditType type) {
		super();
		this.dialog = dialog;
		result = data;
		this.user = user;
		this.type = type;
		initComponents();
		initListeners();
		initValues();
	}
	
	
	private void initValues() {
		date.setDate(result.getDate().toDate());
		startTimeSpinner.setValue(result.getStartTime().toDate());
		endTimeSpinner.setValue(result.getEndTime().toDate());
		note.setText(result.getNote());
	}

	public void pack() {
		result.setUserId(user.getId());
		result.setDate(new LocalDate(date.getDate().getTime()));
		result.setStartTime(new DateTime((Date) startTimeSpinner.getValue()));
		result.setEndTime(new DateTime((Date) endTimeSpinner.getValue()));
		result.setNote(note.getText());
	}
	private void initListeners() {
		okButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				pack();
				UserDataServicesProxy proxy = new UserDataServicesProxy();
				try {
					switch (type) {
					case ADD:
						int setData = proxy.addData(result.toUserData());
						if(setData == -1) {
							JOptionPane.showMessageDialog(null, "Не удалось сохранить данные в БД");
							return;
						} else {
							result.setId(setData);
						}
						break;
					case EDIT:
						proxy.updateData(result.toUserData());
						break;

					default:
						break;
					}
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				dialog.dispose();
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				result = null;
				dialog.dispose();
			}
		});
	}


	private void initComponents() {
		JPanel panel = new JPanel(new GridBagLayout());
		panel.add(new JLabel("Дата"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		date = new JDateChooser(DateTime.now().toDate());
		panel.add(date, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		panel.add(new JLabel("Проснулся"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		SpinnerModel model = new SpinnerDateModel();
		startTimeSpinner = new JSpinner(model);
		JComponent editor = new JSpinner.DateEditor(startTimeSpinner, "yyyy-MM-dd HH:mm:ss");
		startTimeSpinner.setEditor(editor);
		panel.add(startTimeSpinner, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		panel.add(new JLabel("Лёг спать"), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		model = new SpinnerDateModel();
		endTimeSpinner = new JSpinner(model);
		editor = new JSpinner.DateEditor(endTimeSpinner, "yyyy-MM-dd HH:mm:ss");
		endTimeSpinner.setEditor(editor);
		panel.add(endTimeSpinner, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		panel.add(new JLabel("Примечание"), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		panel.add(note, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 5), 0, 0));
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		panel.add(buttonPanel, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		add(panel);
	}



	public UserDataWrapper getResult() {
		return result;
	}

	public static UserDataWrapper showDialog(User user, UserDataWrapper data, EditType type) {
		JDialog dialog = new JDialog();
		UserDataEditDialog authDialog = new UserDataEditDialog(dialog, user, data, type);
		dialog.add(authDialog);
		if(type.equals(EditType.ADD))
			dialog.setTitle("Добаление");
		else
			dialog.setTitle("Редактирование");
		dialog.setModal(true);
		dialog.setLocationRelativeTo(null);
		dialog.pack();
		dialog.setVisible(true);
		return authDialog.getResult();
	}
}
